package com.maxsalad.uc.controller;

import com.maxsalad.common.message.IMessage;
import com.maxsalad.common.message.MessageResult;
import com.maxsalad.uc.pojo.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ObjectError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-01-26.
 */
public class BaseController {
    @Autowired
    protected IMessage messages;

    protected ResponseResult getMessage(MessageResult mr) {
        String messageKey = mr.getCode();
        ResponseResult responseResult = new ResponseResult(messages);
        if (messageKey.startsWith("S")) {
            responseResult = getSucMessage(messageKey, mr.getParams());
            responseResult.setResult(mr.getResult());
            return responseResult;
        } else if (messageKey.startsWith("E")) {
            return getFaultMessage(messageKey, mr.getParams());
        }

        return responseResult;
    }

    protected ResponseResult getMessage(String messageKey) {
        return getMessage(MessageResult.getMessage(messageKey, null));
    }


    protected ResponseResult getSucMessage() {
        ResponseResult responseResult = new ResponseResult(messages);
        responseResult.setSucMessage();
        return responseResult;
    }

    protected ResponseResult getSucMessage(String messageKey, Object... params) {
        ResponseResult responseResult = new ResponseResult(messages);
        responseResult.setSucMessage(messageKey, params);
        return responseResult;
    }

    protected ResponseResult getFaultMessage() {
        ResponseResult responseResult = new ResponseResult(messages);
        responseResult.setFaultMessage("");
        return responseResult;
    }

    protected ResponseResult getFaultMessage(String messageKey, Object... params) {
        ResponseResult responseResult = new ResponseResult(messages);
        responseResult.setFaultMessage(messageKey, params);
        return responseResult;
    }

    protected ResponseResult getFaultResultData(Object obj) {
        ResponseResult responseResult = new ResponseResult(messages);
        responseResult.setFaultResult(obj);
        return responseResult;
    }

    protected ResponseResult getSucResultData(Object obj) {
        ResponseResult responseResult = new ResponseResult(messages);
        responseResult.setMessage("S00001");
        responseResult.setResult(obj);
        return responseResult;
    }

    /**
     * 不需要分页的
     *
     * @param list
     * @return
     */
    protected ResponseResult getSucResultData(List list) {
        Map map = new HashMap();
        map.put("rows", list);
        return getSucResultData(map);
    }


    protected ResponseResult getValidatorFaultMessage(List<ObjectError> list) {
        ResponseResult responseResult = new ResponseResult(messages);
        return responseResult;
    }


    protected ResponseResult getValidatorFalutMessage() {
        ResponseResult responseResult = new ResponseResult(messages);
        responseResult.setFaultMessage("E00077");
        return responseResult;
    }

    protected ResponseResult chooseMessageByKey(String messageKey) {
        if (StringUtils.isBlank(messageKey)) {
            return getSucMessage();
        } else {
            return getMessage(messageKey);
        }
    }
}
